package com.fkframe.codegener.ui.view;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.table.AbstractTableModel;

/**
 * 简单的属性设置table model
 * @author AGUU
 *
 */
public class SimplePropTableModel extends AbstractTableModel{
	private Map<String,String> columnMap = new LinkedHashMap<String,String>();
	private List<Map<String,Object>> data = new ArrayList<Map<String,Object>>() ;
	
	public void setColumnMap(LinkedHashMap<String,String> map){
		columnMap.clear();
		columnMap.putAll(map);
	}
	
	public void setData(List<Map<String,Object>> adata){
		data.clear();
		data.addAll(adata);
	}
	
	public int getColumnCount() {
		return columnMap.size();
	}

	public int getRowCount() {
		return data.size();
	}

	public String getColumnName(int col) {
		int i=0;
		for(Entry<String, String> entry: columnMap.entrySet()){
			if(i==col)return entry.getValue();
			i++;
		}
		return "";
	}

	public Object getValueAt(int row, int col) {
		String key = getColumnKey(col);
		Object result = data.get(row).get(key);
		return result;
	}
	
	public String getColumnNameByKey(String key){
		return columnMap.get(key);
	}
	
	private String getColumnKey(int col){
		int i=0;
		for(Entry<String, String> entry: columnMap.entrySet()){
			if(i==col)return entry.getKey();
			i++;
		}
		return null;
	}

	/*
	 * JTable uses this method to determine the default renderer/ editor for
	 * each cell. If we didn't implement this method, then the last column
	 * would contain text ("true"/"false"), rather than a check box.
	 */
	public Class getColumnClass(int c) {
		// 实现了如果是boolean自动转成JCheckbox
		/*
		 * 需要自己的celleditor这么麻烦吧。jtable自动支持Jcheckbox，
		 * 只要覆盖tablemodel的getColumnClass返回一个boolean的class，
		 * jtable会自动画一个Jcheckbox给你， 你的value是true还是false直接读table里那个cell的值就可以
		 */
		//System.out.println("==============" + getValueAt(0, c).getClass());
		return getValueAt(0, c).getClass();
	}

	/*
	 * Don't need to implement this method unless your table's editable.
	 */
	public boolean isCellEditable(int row, int col) {
		// Note that the data/cell address is constant,
		// no matter where the cell appears onscreen.
		if(editCols!=null){
			if(editCols.contains(col))return true;
			return false;
		}else{
			if(col>1)return true;
			return false;
		}
	}
	
	private List<Integer> editCols = null;
	public void setEditAbleProp(String[] keys){
		editCols=new ArrayList<Integer>();
		int i=0;
		for(Entry<String, String> entry: columnMap.entrySet()){
			for(String key :keys){
				if(entry.getKey().equals(key)){
					editCols.add(i);
				}
			}
			i++;
		}
	}
	
	/*
	 * Don't need to implement this method unless your table's data can
	 * change.
	 */
	public void setValueAt(Object value, int row, int col) {
		Map<String,Object> map = data.get(row);
		String key = this.getColumnKey(col);
		map.put(key, value);
	}
	
	public List<Map<String,Object>> getTableData(){
		return this.data;
	}
}